package com.qf.j2102.controller;


import com.qf.j2102.common.JsonBean;
import com.qf.j2102.po.SysUser;
import com.qf.j2102.service.SysOrderService;
import com.qf.j2102.utils.JsonUtils;
import com.qf.j2102.vo.VOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class SysOrderController {

    @Autowired
    private SysOrderService orderService;


    @RequestMapping("/add.do")
    @ResponseBody
    public JsonBean addOrder(HttpSession session, Integer cid, Integer getid, Integer backid, String oprice) {


        JsonBean bean = null;
        SysUser user = (SysUser) session.getAttribute("user");
        if(user == null){
            bean = JsonUtils.createJsonBean(2,"您未登录！");

        }else {

            try {
                orderService.addOrder(user.getId(),cid, getid, backid, oprice);
                bean = JsonUtils.createJsonBean(1,"预定成功！");
            } catch (Exception e) {
                bean = JsonUtils.createJsonBean(0,e.getMessage());
            }
        }

        return bean;

    }

    @RequestMapping("/all.do")
    @ResponseBody
    public JsonBean selectAll(HttpSession session) {
        JsonBean bean = null;
        SysUser user = (SysUser) session.getAttribute("user");
        List<VOrder> list= orderService.selectAllV(user.getId());
        try {
            /* Map<String, Object> map = orderService.selectAllV(user.getId(), page);*/

            bean = JsonUtils.createJsonBean(1,list);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }

        return bean;

    }

    @RequestMapping("/del.do")
    @ResponseBody
    public JsonBean del(HttpSession session,Integer id) {
        JsonBean bean = null;
        SysUser user = (SysUser) session.getAttribute("user");
        List<VOrder> list= orderService.selectAllV(user.getId());
        list.remove(id);
        int del = orderService.del(id);
        try {
            /* Map<String, Object> map = orderService.selectAllV(user.getId(), page);*/

            bean = JsonUtils.createJsonBean(1,list);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }

        return bean;

    }




}
